package com.leetcode.math;

import java.util.HashSet;
import java.util.Set;

public class MatrixSumQueries {
    public static void main(String[] args) {

    }

    public static long solve(int n, int[][] queries) {
        long res = 0;
        Set<Integer>[] vis = new Set[]{new HashSet<>(), new HashSet<>()};
        for (int i = queries.length - 1; i >= 0; i--) {
            int type = queries[i][0];
            int ind = queries[i][1];
            int val = queries[i][2];
            if (!vis[type].contains(ind)) {
                res += (long)(n - vis[type ^ 1].size()) * val;
                vis[type].add(ind);
            }
        }
        return res;
    }
}
